package com.foreverwin.jzymes.production.dismantle.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.domain.CarrierBind;
import com.foreverwin.jzymes.common.domain.CarrierScanLog;
import com.foreverwin.jzymes.common.domain.NcData;
import com.foreverwin.jzymes.common.service.ICarrierBindAdviceService;
import com.foreverwin.jzymes.common.service.ICommonService;
import com.foreverwin.jzymes.common.service.INCDataAdviceService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.event.AppEventPublisher;
import com.foreverwin.jzymes.production.dismantle.service.IDismantleAdviceService;
import com.foreverwin.jzymes.production.inspect.service.IInspectAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DismantleAdviceServiceImpl implements IDismantleAdviceService {

    @Autowired
    ICarrierBindAdviceService carrierBindAdviceService;
    @Autowired
    IInspectAdviceService inspectAdviceService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    ICommonService commonService;
    @Autowired
    INCDataAdviceService ncDataAdviceService;
    @Autowired
    AppEventPublisher appEventPublisher;

    /**
     * 拆夹扫码过站
     *
     * @param carrierScanLog
     */
    @Transactional( propagation = Propagation.REQUIRED, rollbackFor = Exception.class )
    public void dismantleCarrier( CarrierScanLog carrierScanLog ){

        /**查询绑定信息*/
        List<CarrierBind> carrierBindList = carrierBindAdviceService.existCarrierBindonBind( carrierScanLog.getSite(), carrierScanLog.getCarrier() );

        /**检查是否在全检工序排队，全检不良处置会自动过站只做解绑*/
        String operation = resourceAdviceService.findOperationByResource( carrierScanLog.getSite(), carrierScanLog.getResrce() );
        /*if( commonService.checkCarrierPassOnOperation( carrierBindList.get(0), operation ) ){
            carrierBindAdviceService.carrierUnBind( carrierScanLog.getSite(), carrierScanLog.getCarrier(), carrierScanLog.getSendTime() );
            return;
            //throw BusinessException.build( "production.nc.carrierPassOnOperation", new String[]{ carrierScanLog.getCarrier(), operation } );
        }*/

        if( !commonService.checkCarrierPassOnOperation( carrierBindList.get(0), operation ) && !carrierBindList.get( 0 ).getNextOperation().equals( operation ) ){
            //throw BusinessException.build( "common.carrierNotPassOnOperation", new String[]{ carrierScanLog.getCarrier(), carrierBindList.get( 0 ).getNextOperation(), operation } );
            /**全检漏扫本体过站*/
            missInspectCarrierPass( carrierScanLog.getSite(), carrierScanLog.getCarrier(), operation );
        }

        /**检查本体并过站*/
        inspectAdviceService.inspectCarrier( carrierScanLog );

        /**本体解绑*/
        carrierBindAdviceService.carrierUnBind( carrierScanLog.getSite(), carrierScanLog.getCarrier(), carrierScanLog.getCreatedDateTime() );
    }

    /**
     * 扫描遗漏本体过站
     *
     * @param site
     * @param carrier
     */
    public void missInspectCarrierPass( String site, String carrier, String operation ){
        List<CarrierBind> carrierBindList = carrierBindAdviceService.existCarrierBindonBind( site, carrier );

        /**全检工序*/
        String prevOperation = commonService.findSfcPrevOperation( site, carrierBindList.get(0).getItemBo(), operation );
        /**全检工序默认资源*/
        String resrce = commonService.findOperationDefaultResource( site, prevOperation );

        List<NcData> ncDatas = ncDataAdviceService.findOpenNcDataFromDate( site, carrier );
        if( ncDatas == null || ncDatas.size() == 0 ){
            /**本体过站*/
            commonService.sfcPassByCarrier( site, carrier, resrce, new Date() );

            JSONObject eventData = new JSONObject();
            eventData.put( "SITE", site );
            eventData.put( "CARRIER", carrier );
            eventData.put( "RESRCE", resrce );
            AppEvent<JSONObject> appEvent = new AppEvent<>( this );
            appEvent.setName( "CARRIER_CHECK_OUT_EVENT" );
            appEvent.setData( eventData );
            appEventPublisher.publishEvent( appEvent );
        }



    }
}
